package com.chushouya.manager.service.admin.impl;

import com.chushouya.common.support.Contexts;
import com.chushouya.common.support.RequestHeaderContext;
import com.general.framework.core.constant.Constants;
import com.general.framework.core.enums.BusinessStatus;
import com.general.framework.core.enums.ContextUserType;
import com.general.framework.core.lang.Dates;
import com.general.framework.core.lang.Lists;
import com.general.framework.core.lang.Strings;
import com.general.framework.web.utils.IpAddressUtil;
import com.chushouya.manager.dao.entity.LoginLogEntity;
import com.chushouya.manager.dao.repository.LoginLogRepository;
import com.chushouya.manager.dto.admin.log.LoginLogDTO;
import com.chushouya.manager.dto.admin.log.LoginLogListDTO;
import com.chushouya.manager.dto.admin.log.LoginLogQueryDTO;
import com.chushouya.manager.service.admin.LoginLogService;
import com.general.framework.core.lang.Beans;
import com.general.framework.core.schema.PageResult;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统访问日志情况信息 服务层处理
 * @author leejean team
 */
@Service
public class LoginLogServiceImpl implements LoginLogService {

    @Resource
    private LoginLogRepository loginLogRepository;


    @Override
    public void clean(Integer cleanBeforeDays) {
        loginLogRepository.cleanLogin(cleanBeforeDays);
    }

    @Override
    public PageResult<LoginLogListDTO> selectLoginInfoPageResult(LoginLogQueryDTO query) {
        return PageResult.of(loginLogRepository.selectLoginInfoPageResult(query), LoginLogListDTO.class);
    }

    @Override
    public void saveLoginLog(ContextUserType userType, String userName, String status, String message) {
        final RequestHeaderContext header = Contexts.getHeaderContext();
        LoginLogDTO loginLog = new LoginLogDTO();
        loginLog.setUserType(userType.value());
        loginLog.setUserName(userName);

        loginLog.setLoginIp(header.getIp());
        loginLog.setLoginLocation(IpAddressUtil.getAddress(header.getIp()));
        List<String> loginDetailList = Lists.newArrayList();
        if (Strings.isNotEmpty(header.getOsType())) {
            loginDetailList.add("osType:".concat(header.getOsType()));
        }
        if (Strings.isNotEmpty(header.getOsVersion())) {
            loginDetailList.add("osVersion:".concat(header.getOsVersion()));
        }
        if (Strings.isNotEmpty(header.getPlatform())) {
            loginDetailList.add("platform:".concat(header.getPlatform()));
        }
        if (Strings.isNotEmpty(header.getAppVersion())) {
            loginDetailList.add("appVersion:".concat(header.getAppVersion()));
        }
        if (Strings.isNotEmpty(header.getDeviceId())) {
            loginDetailList.add("deviceId:".concat(header.getDeviceId()));
        }
        loginLog.setLoginDetail(Strings.join(loginDetailList, ","));
        if (Strings.equalsAny(status, Constants.LOGIN_SUCCESS, Constants.REGISTER, Constants.LOGOUT)) {
            loginLog.setStatus(BusinessStatus.SUCCESS.getCode());
        } else if (Constants.LOGIN_FAIL.equals(status)) {
            loginLog.setStatus(BusinessStatus.FAIL.getCode());
        }
        loginLog.setMsg(message);
        loginLog.setLoginTime(Dates.getTimeNow());

        loginLogRepository.saveOperateLog(Beans.copy(loginLog, LoginLogEntity.class));
    }

    @Async
    @Override
    public void saveLoginLog(LoginLogDTO log) {
        loginLogRepository.saveOperateLog(Beans.copy(log, LoginLogEntity.class));
    }

}
